package com.ph.custom.processor.handler;

import org.springframework.beans.factory.annotation.Autowired;

import com.ph.custom.cache.service.CustomTokenService;
import com.ph.custom.cenum.TokenCheckEnum;
import com.ph.custom.cenum.UserCacheEnum;
import com.ph.custom.processor.CacheDataProcessor;
import com.ph.custom.processor.UserCacheProcessor;
/**
 * 
 * @ClassName:  CacheProcessorHandler   
 * @Description:缓存处理
 * @author: lijie
 * @date:   2017年4月15日 下午3:07:53     
 * @Copyright: 2017
 */
public class CacheProcessorHandler implements UserCacheProcessor {

	@Autowired
	private CacheDataProcessor cacheDataProcessor;

	@Autowired
	private CustomTokenService tokenService;

	@Override
	public <T> T userCachePreHandle(UserCacheEnum uenum, Class<T> clas, Object resource, Object obj) throws Throwable {
		T t = null;
		if (uenum != null) {
			switch (uenum) {
			case INSERT:
				cacheDataProcessor.insertCache(resource, obj);
				break;
			case UPDATE:
				cacheDataProcessor.updateCache(resource, obj);
				t = cacheDataProcessor.selectCache(clas, obj);
				break;
			case DELETE:
				cacheDataProcessor.deleteCache(obj);
				break;
			default:
				t = cacheDataProcessor.selectCache(clas, obj);
				break;
			}
		}
		return t;
	}

	@Override
	public String tokenPreHandle(TokenCheckEnum tuenm, String token) throws Throwable {
		String result = null;
		if (tuenm != null) {
			switch (tuenm) {
			case PUT:
				tokenService.putToken(token);
				break;
			default:
				result = tokenService.getToken(token);
				break;
			}
		}
		return result;
	}
}
